#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/2/3 16:15
# @Author  : Ramsey
# @Site    : zh
# @File    : log.py
# @Software: PyCharm
from logging import Formatter, StreamHandler, INFO, Logger

LOG_FORMAT = "%(asctime)s [%(name)s] %(levelname)s: %(message)s"


class LoggerManager:
    logger: dict = {}

    @classmethod
    def get_logger(cls, name: str = "default", log_level=None, log_format=LOG_FORMAT):
        key = f"{name}_{log_level}"

        def generate_logger():
            logger_formatter = Formatter(log_format)
            handler = StreamHandler()
            handler.setFormatter(logger_formatter)
            handler.setLevel(log_level or INFO)
            _logger = Logger(name)
            _logger.addHandler(handler)
            _logger.setLevel(log_level or INFO)

            cls.logger[key] = _logger
            return _logger

        return cls.logger.get(key, None) or generate_logger()


spider_logger = LoggerManager.get_logger

if __name__ == "__main__":
    LoggerManager.get_logger(name="xxx")
